package de.derhess.projects.pkr.controller
{	
	import de.derhess.projects.pkr.models.CourtCost;
	import de.derhess.projects.pkr.models.PKRBaseData;
	import de.derhess.projects.pkr.models.PKRModel;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import mx.events.PropertyChangeEvent;
	
	/**
	 * released under MIT License (X11)
	 * http://www.opensource.org/licenses/mit-license.php
	 * 
	 * Class  ControllerPKR
	 * 
	 * @author Florian Weil [derhess.de, Deutschland]
	 * @see http://blog.derhess.de
	 */
	public class ControllerPKR
	{
		//--------------------------------------------------------------------------
		//
		//  Class variables
		//
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		private var model:PKRModel
		
		public var controllerBaseData:ControllerPKRBaseData;
		public var controllerOutOfCourt:ControllerPKROutOfCourt;
		public var controllerCourtCost:ControllerPKRCourtLawyerCost;
		
		//--------------------------------------------------------------------------
		//
		//  Initialization
		//
		//--------------------------------------------------------------------------
		public function ControllerPKR(data:PKRModel)
		{
			model = data;
			
			this.model.baseData.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE,handleNewFees);
			this.model.outOfCourt.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE,handleNewOutOfCourt);
			this.model.courtCosts.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE,handleNewCourtCost);
			
			this.controllerBaseData = new ControllerPKRBaseData(this.model.baseData);
			this.controllerOutOfCourt = new ControllerPKROutOfCourt(this.model.outOfCourt);
			this.controllerCourtCost = new ControllerPKRCourtLawyerCost(this.model.courtCosts);
		}
		
		public function init():void
		{
			this.controllerBaseData.setStartValues();
			
			this.controllerOutOfCourt.setStartValues(this.model.baseData.lawyerFee,
													this.model.baseData.amountClients,
													this.model.baseData.rateVAT);
			
			this.controllerCourtCost.setStartValues(this.model.baseData.lawyerFee,
													this.model.baseData.courtFee,
													this.model.baseData.rateVAT);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		
		
		//--------------------------------------------------------------------------
		//
		//  Additional getters and setters
		//
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		public function computeRisk():void 
		{
			this.model.allRiskSum = this.model.outOfCourt.sum + this.model.courtCosts.allCostSum;
		}
		
		public function toString():String 
		{
			return "[ControllerPKR] : model: " + model.toString();	
		}
		//--------------------------------------------------------------------------
		//
		//  API
		//
		//--------------------------------------------------------------------------
		
		
		
		//--------------------------------------------------------------------------
		//
		//  Eventhandling
		//
		//--------------------------------------------------------------------------
		private function handleNewFees(e:PropertyChangeEvent):void
		{
			controllerOutOfCourt.setLawyerFee(this.model.baseData.lawyerFee);
			controllerCourtCost.setLawyerFee(this.model.baseData.lawyerFee);
			controllerCourtCost.setCourtCost(this.model.baseData.courtFee);
			
			computeRisk();
		}
		
		private function handleNewOutOfCourt(e:PropertyChangeEvent):void
		{
			computeRisk();
		}
		
		private function handleNewCourtCost(e:PropertyChangeEvent):void
		{
			computeRisk();
		}
		//--------------------------------------------------------------------------
		//
		//  Broadcasting
		//
		//--------------------------------------------------------------------------
		
		
	}
}